﻿Imports Training.Common

Partial Public Class EditGroup
    Inherits System.Web.UI.Page
    Public gName As String ' group name
    Dim gid As Integer ' groupid
    Dim oLog As String  ' original login name

    ''' <summary>
    ''' Page load event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for querystring data
        If IsNothing(Request.QueryString("gid")) Or IsNothing(Request.QueryString("gnm")) Then
            Response.Redirect("Groups.aspx")
        Else
            gid = Request.QueryString("gid")
            gName = Request.QueryString("gnm")
        End If

        If Not Page.IsPostBack Then
            ' load Group info
            LoadGroupFields()
        Else
            ' get login name
            oLog = GetGroupLogin(gid)
            ' check for check boxes to enable/disable
            If cbPassword.Checked Then
                EnablePassword()
            Else
                DisablePassword()
            End If

            If cbEmail.Checked Then
                EnableEmail()
            Else
                DisableEmail()
            End If

            If cbLogo.Checked Then
                EnableFileUpload()
            Else
                DisableFileUpload()
            End If
        End If
    End Sub

    ''' <summary>
    ''' Enable password fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnablePassword()
        tbPass.Enabled = True
        rfPass.Enabled = True
        tbPConf.Enabled = True
        rfPConf.Enabled = True
        cvPassword.Enabled = True
    End Sub

    ''' <summary>
    ''' Disable password fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisablePassword()
        tbPass.Enabled = False
        rfPass.Enabled = False
        tbPConf.Enabled = False
        rfPConf.Enabled = False
        cvPassword.Enabled = False
    End Sub

    ''' <summary>
    ''' Enable email fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableEmail()
        tbEmail.Enabled = True
        rfEmail.Enabled = True
        reEmail.Enabled = True
        tbEConf.Enabled = True
        rfEConf.Enabled = True
        cvEmail.Enabled = True
    End Sub

    ''' <summary>
    ''' Disable email fields and validators
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisableEmail()
        tbEmail.Enabled = False
        rfEmail.Enabled = False
        reEmail.Enabled = False
        tbEConf.Enabled = False
        rfEConf.Enabled = False
        cvEmail.Enabled = False
    End Sub

    ''' <summary>
    ''' Enable file upload and validator
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableFileUpload()
        rfLogo.Enabled = True
        fuLogo.Enabled = True
    End Sub

    ''' <summary>
    ''' Disable file upload and validator
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisableFileUpload()
        rfLogo.Enabled = False
        fuLogo.Enabled = False
    End Sub

    ''' <summary>
    ''' Load fields with Group Information
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadGroupFields()
        ' get login name
        oLog = GetGroupLogin(gid)
        tbLogin.Text = oLog
        tbGroup.Text = gName
    End Sub

    ''' <summary>
    ''' UpdateButton click event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnUpdateGroup_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateGroup.Click
        If Page.IsValid Then
            ' group name change
            ChangeGroupName()

            ' email change
            ChangeEmail()

            ' password change
            ChangePassword()

            ' Update logo
            LogoUpdate()
        Else
            lblReturn.Text = "Error validating form data."
        End If

    End Sub

    ''' <summary>
    ''' Check if updating GroupName is required, if so do it
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ChangeGroupName()
        lblReturn.Text = ""

        ' check if group name was changed
        If Trim(gName) <> Trim(tbGroup.Text) Then
            ' update group name
            If UpdateGroupName(tbGroup.Text, gid) Then
                lblReturn.Text &= "Group name has been changed to " & tbGroup.Text & " "
            Else
                lblReturn.Text &= "There was a problem changing group name. "
            End If
        End If
    End Sub

    ''' <summary>
    ''' Check email for duplicates
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckEmail() As String
        Dim retVal As String

        ' set inital value
        retVal = ""

        ' check email
        If CheckMemberEmail(tbEmail.Text) > 0 Then
            retVal &= " Email address is in use, please enter a different Email address."
        End If

        Return retVal
    End Function

    ''' <summary>
    ''' Update group email address
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ChangeEmail()
        If cbEmail.Checked Then
            ' check email for dupes
            If Len(CheckEmail()) <> 0 Then
                ' dupe encountered show message
                lblReturn.Text = CheckEmail()
            Else
                ' email is unique update
                Dim user As MembershipUser
                user = Membership.GetUser(oLog)
                user.Email = Trim(tbEmail.Text)
                Membership.UpdateUser(user)

                lblReturn.Text &= " Email has been updated."
            End If
        End If
    End Sub

    ''' <summary>
    ''' Update group password
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ChangePassword()
        If cbPassword.Checked Then
            ChangeMembershipPassword(oLog, Trim(tbPass.Text))

            lblReturn.Text &= " Password has been updated."
        End If
    End Sub

    ''' <summary>
    ''' Upload logo and update db
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LogoUpdate()
        If cbLogo.Checked Then
            Dim lfile As String
            lfile = UploadFile()

            If lfile = String.Empty Then
                lblReturn.Text &= " logo upload failed."
            Else
                If UpdateGroupLogo(gid, lfile) = 1 Then
                    lblReturn.Text &= " logo was uploaded and updated."
                Else
                    lblReturn.Text &= " logo information update failed."
                End If
            End If
        End If
    End Sub

    ''' <summary>
    '''  upload the logo file
    ''' </summary>
    ''' <remarks></remarks>
    Private Function UploadFile() As String
        Dim logo As String
        Dim fn As String ' file name
        Dim ct As String ' file content
        Dim fz As String ' file size
        Dim fileUploaded As Boolean

        ' initial value of fileUploaded
        fileUploaded = False

        fn = fuLogo.PostedFile.FileName
        ct = fuLogo.PostedFile.ContentType
        fz = fuLogo.PostedFile.ContentLength

        ' define path to upload folder
        Dim p As String
        p = Server.MapPath("../images/logos")

        Try
            ' upload the actual file
            fuLogo.PostedFile.SaveAs(p & "\" & System.IO.Path.GetFileName(fn.ToString()))
            ' set fileUploaded
            fileUploaded = True

            ' check if upload completed
            If fileUploaded Then
                ' get file type and path
                logo = System.IO.Path.GetFileName(fn.ToString())
            Else
                ' upload failed, notify user
                logo = String.Empty
            End If
        Catch ex As Exception
            ' also log error here
            log.Error("Error uploading logo * " & ex.ToString)
            logo = String.Empty
        End Try

        Return logo
    End Function
End Class